package com.semanticbits.codejam.model

import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.auth.*
import org.apache.commons.httpclient.methods.*
/**
 * Created by IntelliJ IDEA.
 * User: carlos.perez
 * Date: 1/15/11
 * Time: 12:53 PM
 * To change this template use File | Settings | File Templates.
 */
class HudsonClient {


  def String hudsonHost = app.Application.HUDSON_URL
  def String projectName = "TEST00"
  def String username = "username"
  def String password = "password"
  def String config_xml =
"""


""";

   void createProject() {


  def client = new HttpClient()

  /*
  client.state.setCredentials(
    new AuthScope( server, 443, "realm"),
    new UsernamePasswordCredentials( username, password )
  )
  */

  // Hudson does not do any authentication negotiation,
  // ie. it does not return a 401 (Unauthorized)
  // but immediately a 403 (Forbidden)
  //client.params.authenticationPreemptive = true

  def url =   "${hudsonHost}/createItem?name=${projectName}"
  println url

  def post = new PostMethod( url )
  post.doAuthentication = false; //true

  //File input = new File(configurationFile);
  def input = config_xml;
  RequestEntity entity = new StringRequestEntity(input, "text/xml", "UTF-8");
  post.setRequestEntity(entity);
  try {
    int result = client.executeMethod(post)
    println "Return code: ${result}"
    post.responseHeaders.each{ println it.toString().trim() }
    println post.getResponseBodyAsString()
  } finally {
    post.releaseConnection()
  }
}


   boolean checkProject() {

  int result = 0;
  def client = new HttpClient()

  /*
  client.state.setCredentials(
    new AuthScope( server, 443, "realm"),
    new UsernamePasswordCredentials( username, password )
  )
  */

  // Hudson does not do any authentication negotiation,
  // ie. it does not return a 401 (Unauthorized)
  // but immediately a 403 (Forbidden)
  //client.params.authenticationPreemptive = true

  def url =   "${hudsonHost}/job/${projectName}"
  println url

  def get = new GetMethod( url )
  get.doAuthentication = false; //true

  //File input = new File(configurationFile);
  try {
    result = client.executeMethod(get)
    //println "Return code: ${result}"
    //println get.getResponseBodyAsString()
  } finally {
    get.releaseConnection()
  }


  if( result != 200 ) return false; else return true;
}


  void build() {

  int result = 0;
  def client = new HttpClient()

  /*
  client.state.setCredentials(
    new AuthScope( server, 443, "realm"),
    new UsernamePasswordCredentials( username, password )
  )
  */

  // Hudson does not do any authentication negotiation,
  // ie. it does not return a 401 (Unauthorized)
  // but immediately a 403 (Forbidden)
  //client.params.authenticationPreemptive = true

  def url =   "${hudsonHost}/job/${projectName}/build"
  println url

  def get = new GetMethod( url )
  get.doAuthentication = false; //true

  //File input = new File(configurationFile);
  try {
    result = client.executeMethod(get)
    //println "Return code: ${result}"
    //println get.getResponseBodyAsString()
  } finally {
    get.releaseConnection()
  }
}


   void deleteProject() {


  def client = new HttpClient()

  /*
  client.state.setCredentials(
    new AuthScope( server, 443, "realm"),
    new UsernamePasswordCredentials( username, password )
  )
  */

  // Hudson does not do any authentication negotiation,
  // ie. it does not return a 401 (Unauthorized)
  // but immediately a 403 (Forbidden)
  //client.params.authenticationPreemptive = true

  def url =   "${hudsonHost}/job/${projectName}/doDelete"    // http://localhost:8081/hudson/job/test/doDelete
  println url

  def post = new PostMethod( url )
  post.doAuthentication = false; //true

  //File input = new File(configurationFile);
  RequestEntity entity = new StringRequestEntity("", "text/xml", "UTF-8");
  post.setRequestEntity(entity);
  try {
    int result = client.executeMethod(post)
    println "Return code: ${result}"
    post.responseHeaders.each{ println it.toString().trim() }
    println post.getResponseBodyAsString()
  } finally {
    post.releaseConnection()
  }
}

}


